package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.entity.Sku;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <h3>leyou-project</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-05-28 12:05
 **/
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {
    @Autowired
    private SkuMapper mapper;


    @Override
    @Transactional
    public void deductStock(Map<Long, Integer> cartMap) {
        /**
         * 批量减库存
         */
        try {

            executeBatch(sqlSession -> {
                for (Map.Entry<Long, Integer> entry : cartMap.entrySet()) {
                    Integer num = entry.getValue();
                    Long id = entry.getKey();
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("id", id);
                    map.put("num", num);
                    sqlSession.update("com.leyou.item.mapper.SkuMapper.deDuctStock", map);
                }
                sqlSession.flushStatements();
            });
        } catch (Exception e) {
            throw new LyException(400, "手速慢啦");
        }


    }
}
